// JavaScript Document

var thumb_flag = 0;
var limit_gallery = 0;

var numberPreviews = 0;
var countLoadPreviews = 0;


$(document).ready(function(){

if($("body").hasClass('page-gallery')){
	$("div.block-core-ItemInfo *").remove();
}

if($("div#container").hasClass("gallery") && !$('body').hasClass('page-gallery')){  
  initPreview();
}
else if ($("div#container").hasClass("gallery") && $('body').hasClass('page-gallery')) {
  initPreviewMainPage();
}

if($("body").hasClass("node-type-clipping")){
  $("div#content div.content:first").append("<p><a href='javascript:history.back()'>Back to News</a></p>");
}

var fs = 100;

$("a.zoomout").click(function(){
	if(fs > 100 ){
		fs = fs-20;
		$("div#content").css("font-size",fs+"%");
	}
});

$("a.zoomin").click(function(){
	if((fs < 160) || (fs <= 100 )){
		fs = fs + 20;
		$("div#content").css("font-size",fs+"%");
	}
});


$("li.bookmark a").click(function(){
	window.external.AddFavorite("http://www.patkaufmanart.com",$('h1').text());
});

createLeafs($("ul.menu li", "#block-menu-primary-links"));	


/*
 * Set the default action when users press
 * enter key on cart pages
 */
if($("table.checkoutButtonTable").length > 0) {  
  $('form input').keypress(function (e) {
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
      $('input[type=submit].checkoutSaveButton').click();
      return false;
    } else {
      return true;
    }
  });
  
  // change the label of the cart button
  $('input[type=submit].checkoutSaveButton').val(Drupal.t('Update Cart'));
}

// change the Prices labels on photo's page
$('div.block-checkout-ItemPrices h3').text(Drupal.t('Sizes and Prices'));

// change remove button behavior on cart page
changeRemoveProductButton();

}); 


function initPreviewMainPage() {
  var count = 1;
  var totalGalleries = $(".giAlbumCell").length;
  
  $(".giAlbumCell").each(function(){    
    context = $(this);
    
    $('div a img', context).css('visibility', 'hidden');
    appendLoaderImage(context);
    
    $('div.block-core-ItemInfo', context).load(Drupal.settings.site_path + $('a' , context).attr('href')+ " #gsThumbMatrix img:lt(3)", function() {
      var my_context = $(this).parent();

      processPreview(my_context);
      
      if(count == totalGalleries) {
        startScrollItensMainPage();
      }
      else {
        count++;
      }
    });
  });
}



function initPreview(){ 
  $(".giAlbumCell").mouseover(function(){
    
    if($(this).hasClass('processed')) {
      return;
    } else {
      $(this).addClass('processed');
    }
    
    context = $(this);
    
    $('div a img', context).css('visibility', 'hidden');
    appendLoaderImage(context);
    
    $('div.block-core-ItemInfo', context).load(Drupal.settings.site_path + $('a' , context).attr('href')+ " #gsThumbMatrix img:lt(3)", function() {
      var my_context = $(this).parent();

      processPreview(my_context);
      startScrollItens(my_context);
    });
  });

}


function processPreview(context){
  if($("div.preview", context).length  == 0){
		
    $('div:first a', context).html("<div id='preview"+thumb_flag+"' class='preview'><div class='img-itens' id='img-items"+thumb_flag+"'>"+
      "</div></div>");
		
    $('div.block-core-ItemInfo.giInfo img', context).each(function(){      			
      $('div.img-itens', context).append($(this));      
    });
    
    thumb_flag++;

   }   
}

function startScrollItens(context) {
    
  if(($("div.img-itens", context).length > 0)) {
    
    $("div.img-itens", context).cycle({ 
      fx: 'scrollLeft', 
      timeout: 5000
    }).addClass('processed');
   
    $('img.loader', context).remove();
 
  }
}


function startScrollItensMainPage() {

if(($("div.img-itens").length > 0)) {
    
    $("div.img-itens").cycle({ 
      fx: 'scrollLeft', 
      timeout: 5000
    }).addClass('processed');
   
    $('img.loader').remove();
 
  }
}


function appendLoaderImage(context) {
  $('<img class="loader" src="/sites/all/themes/patkaufmanart-theme/images/ajax-loader.gif" title="loader" />').appendTo($('div:first', context));  
}

function hideLinks(){
	$("div#support-links").css('z-index','-1');
	$("div#logo").css('z-index','-1');
	$("div#block-menu-primary-links").css('z-index','-1');
}

function showLinks(){
	$("div#support-links").css('z-index','1');
	$("div#logo").css('z-index','1');
	$("div#block-menu-primary-links").css('z-index','1');
}

function closeDialog(){
  $("div.disclaimer").hide();
  $("div#dialog").hide();
  $("html").css("overflow","auto");
}

function createLeafs(menu){
	var header_itens = 	menu.length;
	i=0;
	
	menu.each(function(){
		if(i<=header_itens-1)
			$(this).addClass("leaf"+i);
		i++;
	});	
}

function getSize(size_img){
  var desc = $("p.giDescription").html().split("Sizes and Prices:")[0];
  $.post(Drupal.settings.basePath +"sites/all/themes/patkaufmanart-theme/getContent.php", {
      desc: $("p.giDescription").html().split("Sizes and Prices:")[0],
      size: size_img
    },
    function (data) {
      data = data.split('|');
      location.href = Drupal.settings.basePath +'contact?description=' +data[0]+ '&size=' +data[1];
    }
  );    
  //location.href = 'http://generic-dev.com/dev/contact?description=' +desc+ '&size=' +size_img;
}


function changeRemoveProductButton() {
  if ($('table.checkoutSelectTable').length && $('table.checkoutButtonTable').length) {

    // remove the "remove" label
    var second_tr = $('table.checkoutSelectTable tr').get(1);
    $('th:first', second_tr).text('');

    // add X button to remove the product
    $('tr.gbEven').each(setNewRemoveButton);
    $('tr.gbOdd').each(setNewRemoveButton);    
  }
}

var setNewRemoveButton = function(){  
  close_image = $('<img class="remove-product" src="/sites/all/themes/patkaufmanart-theme/images/empty-cart.png" title="Remove this product" />');
  
  //$('> td:last', $(this)).addClass('cart-last-td');

  $('> td:first', $(this)).append(close_image);

  close_image.click(function(){        
    var td = $(this).parent();
    $('input', td).attr('checked', true);
    $('input.inputTypeSubmit.checkoutSaveButton').click();
  });
};